using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Service.Interfaces;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.DALProxy.Extensions;

namespace ACME.Northwind.DALProxy
{
	/// <summary>
	/// The service implementation for OrderDetail object
	/// </summary>
	public partial class OrderDetailService : IOrderDetailService
	{
		#region IOrderDetailService Members

		/// <summary>
		/// Run a selection by the primary key
		/// </summary>
		public OrderDetailDTO SelectByPrimaryKey(int orderid, int productid)
		{
			OrderDetailDTO retVal = new OrderDetailDTO();
			if (!retVal.SelectUsingPK(orderid, productid)) return null;
			else return retVal;
		}

		/// <summary>
		/// Run the persist for an object
		/// </summary>
		public bool Persist(OrderDetailDTO dto)
		{
			return (dto.Persist() != null);
		}

		/// <summary>
		/// Run the persist for a list
		/// </summary>
		public bool Persist(List<OrderDetailDTO> list)
		{
			return list.Persist();
		}

		/// <summary>
		/// Run a selection for all objects
		/// </summary>
		public List<OrderDetailDTO> RunSelect()
		{
			List<OrderDetailDTO> retVal = new List<OrderDetailDTO>();
			retVal.RunSelect();
			return retVal;
		}

		/// <summary>
		/// Run a selection using LINQ
		/// </summary>
		public List<OrderDetailDTO> RunSelect(string linq)
		{
			List<OrderDetailDTO> retVal = new List<OrderDetailDTO>();
			retVal.RunSelect(linq);
			return retVal;
		}

		/// <summary>
		/// Run a paginated selection using LINQ
		/// </summary>
		public PagedQueryResults<OrderDetailDTO> RunSelect(int pageIndex, int pageSize, bool ascending, string sortColumn, string linq)
		{
			List<OrderDetailDTO> list = new List<OrderDetailDTO>();
			string sortOrder;
			if (ascending)
				sortOrder = "ascending";
			else
				sortOrder = "descending";
			return list.RunSelect(pageSize.ToString(), pageIndex.ToString(), sortOrder, sortColumn, linq);
		}

		/// <summary>
		/// Return the related Order item
		/// </summary>
		public OrderDTO GetOrderItem(OrderDetailDTO item)
		{
			OrderDTO retval = new OrderDTO();
			retval.SelectUsingPK(item.OrderId);
			return retval;
		}

		/// <summary>
		/// Return the related Product item
		/// </summary>
		public ProductDTO GetProductItem(OrderDetailDTO item)
		{
			ProductDTO retval = new ProductDTO();
			retval.SelectUsingPK(item.ProductId);
			return retval;
		}

		/// <summary>
		/// Delete the specified item
		/// </summary>
		public bool Delete(OrderDetailDTO item)
		{
			OrderDetail orderdetail = OrderDetail.SelectUsingPK(item.OrderId, item.ProductId);
			if (orderdetail == null) return false;
			orderdetail.Delete();
			orderdetail.Persist();
			return true;
		}

		#endregion

	}
}
